//
//  XDXTimeTool.m
//  tanhuo
//
//  Created by XuDongXiang on 2019/8/11.
//  Copyright © 2019 scout. All rights reserved.
//

#import "XDXTimeTool.h"

@implementation XDXTimeTool

// 当前的日期
+ (NSString *)currentDate
{
    NSDate *date = [NSDate date];
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
    format.locale = [NSLocale systemLocale];
    format.calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierISO8601];
    NSString *dateStr;
    dateStr = [format stringFromDate:date];
    return dateStr;
}

//当前的年份
+ (NSInteger)currentYear
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay;
    NSDateComponents *components = [calendar components:unitFlags fromDate:[NSDate date]];
    return [components year];
}

//当前的月份
+ (NSInteger)currentMouth
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay;
    NSDateComponents *components = [calendar components:unitFlags fromDate:[NSDate date]];
    return [components month];
}

//当前的号
+ (NSInteger)currentDay
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay;
    NSDateComponents *components = [calendar components:unitFlags fromDate:[NSDate date]];
    return [components day];
}

+ (NSString *)getDate:(NSString *)date
{
    if (date.length>=10) {
        date = [date substringToIndex:10];
        date = [date stringByReplacingCharactersInRange:NSMakeRange(4, 1) withString:@"年"];
        date = [date stringByReplacingCharactersInRange:NSMakeRange(7, 1) withString:@"月"];
        date = [date stringByAppendingString:@"日"];
        
        NSInteger mouth = [[date substringWithRange:NSMakeRange(5, 2)] integerValue];
        NSInteger day = [[date substringWithRange:NSMakeRange(8, 2)] integerValue];
        
        date = [date stringByReplacingCharactersInRange:NSMakeRange(5, 2) withString:[NSString stringWithFormat:@"%ld",(long)mouth]];
        date = [date stringByReplacingCharactersInRange:NSMakeRange(7+(mouth>=10?1:0), 2) withString:[NSString stringWithFormat:@"%ld",(long)day]];
    }
    
    return date;
}

+ (NSString *)getNowTimeTimestamp
{
    NSDate *datenow = [NSDate date];
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
    return timeSp;
}

+ (NSString *)compareCurrentTimeWithDate:(NSString *)aDate
{
    NSString *string = aDate;
    if (string.length >= 19) {
        string = [string substringToIndex:19];
        //把字符串转为NSdate
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        dateFormatter.locale = [NSLocale systemLocale];
        dateFormatter.calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierISO8601];
        NSDate *timeDate = [dateFormatter dateFromString:string];
        
        //得到与当前时间差
        NSInteger  timeInterval = [timeDate timeIntervalSinceNow];
        timeInterval = -timeInterval;
        
        timeInterval = timeInterval + 8*60*60;
        NSString *result;
        if ([[string substringToIndex:10] isEqualToString:[[XDXTimeTool currentDate] substringToIndex:10]]) {
            result = [NSString stringWithFormat:@"今天 %@:%@",[string substringWithRange:NSMakeRange(11, 2)],[string substringWithRange:NSMakeRange(14, 2)]];
        }else{
            NSInteger yesTimetamp = [XDXTimeTool getNowTimeTimestamp].integerValue-86400;
            NSDate *date = [NSDate dateWithTimeIntervalSince1970:yesTimetamp];
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            NSString *dateStr = [formatter stringFromDate:date];
            
            NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
            NSInteger unitFlags = NSCalendarUnitWeekday;
            NSInteger today = [[calendar components:unitFlags fromDate:[NSDate date]] weekday];
            
            if ([[dateStr substringToIndex:10] isEqualToString:[aDate substringToIndex:10]]) {
                result = [NSString stringWithFormat:@"昨天 %@:%@",[string substringWithRange:NSMakeRange(11, 2)],[string substringWithRange:NSMakeRange(14, 2)]];
            }else if (timeInterval<86400*(today-2)){
                NSDateComponents *comps = [[NSDateComponents alloc]init];
                comps = [calendar components:unitFlags fromDate:timeDate];
                NSInteger weekday = [comps weekday]-1;
                if (weekday==0) {
                    weekday = 7;
                }
                result = [NSString stringWithFormat:@"%@%@:%@",[self getWeekDayStringWithDay:weekday],[string substringWithRange:NSMakeRange(11, 2)],[string substringWithRange:NSMakeRange(14, 2)]];
            }
            else if ([[string substringWithRange:NSMakeRange(0, 4)] integerValue] == [XDXTimeTool currentYear]){
                result = [NSString stringWithFormat:@"%ld月%ld日",(long)[[string substringWithRange:NSMakeRange(5, 2)] integerValue],(long)[[string substringWithRange:NSMakeRange(8, 2)] integerValue]];
            }else{
                result = [NSString stringWithFormat:@"%ld年%ld月%ld日",(long)[[string substringWithRange:NSMakeRange(0, 4)] integerValue],(long)[[string substringWithRange:NSMakeRange(5, 2)] integerValue],(long)[[string substringWithRange:NSMakeRange(8, 2)] integerValue]];
            }
        }
        return  result;
    }else{
        return @"";
    }
    
}

+ (NSString *)getWeekDayStringWithDay:(NSInteger)day
{
    if (day==1) {
        return @"周一";
    }else if (day==2){
        return @"周二";
    }else if (day==3){
        return @"周三";
    }else if (day==4){
        return @"周四";
    }else if (day==5){
        return @"周五";
    }else if (day==6){
        return @"周六";
    }else{
        return @"周日";
    }
}

@end
